package com.example.weather

import android.os.Bundle
import android.util.Log
import android.widget.ArrayAdapter
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.weather.weather.Forecast
import com.example.weather.weather.Weather

class Main2Activity : AppCompatActivity() {
    val baseURL = "http://t.weather.itboy.net/api/weather/city/"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main2)
        val textView_city = findViewById<android.widget.TextView>(R.id.textView_city)
        val textView_province = findViewById<android.widget.TextView>(R.id.textView_province)
        val textView_shidu = findViewById<android.widget.TextView>(R.id.textView_shidu)
        val textView_wendu = findViewById<android.widget.TextView>(R.id.textView_wendu)
        val imageView = findViewById<android.widget.ImageView>(R.id.imageView)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        val cityCode = intent.getStringExtra("city_Code")
        val queue = com.android.volley.toolbox.Volley.newRequestQueue(this)
        val stringRequest = com.android.volley.toolbox.StringRequest(baseURL+cityCode,{
            val gson = com.google.gson.Gson()
            val WeatherType = object : com.google.gson.reflect.TypeToken<Weather>(){}.type
            val weather = gson.fromJson<Weather>(it,WeatherType)
            textView_city.text = weather.cityInfo.city
            textView_province.text = weather.cityInfo.parent
            textView_shidu.text = weather.data.shidu
            textView_wendu.text = weather.data.wendu
            val firstDay=weather.data.forecast.first()
            when(firstDay.type){
                "晴"->imageView.setImageResource(R.drawable.sun)
                "多云"->imageView.setImageResource(R.drawable.mcloud)
                "阴"->imageView.setImageResource(R.drawable.cloud)
                "阵雨"->imageView.setImageResource(R.drawable.rain)
                else->imageView.setImageResource(R.drawable.snow)

            }
            val adapter = ArrayAdapter<Forecast>(this,android.R.layout.simple_list_item_1,weather.data.forecast)
            val listView = findViewById<android.widget.ListView>(R.id.listView)
            listView.adapter = adapter

            Log.d("Main2Activity","$it")
        },{
            Log.e("Main2Activity","$it")
        })
        queue.add(stringRequest)
    }


}